import React, { Component } from 'react';
import { Tabs, Breadcrumb, } from 'antd'
import { AppstoreTwoTone } from "@ant-design/icons"
import { connect } from 'dva'
import styles from './index.less'
import TOShareForm from "./shareForm"
import ReceiveForm from './receiveForm'

const { TabPane } = Tabs;
@connect(({global}) => ({
    userId: global.userId,
}))
class ShareForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataOfToMe: [],//分享给我数据
            dataOfFromMe: [],//我的分享
            total: 0
        }
    }

    componentDidMount = () => {

    }
    //分享给我
    getShareListToMe = () => {
        const { dispatch, } = this.props
        dispatch({
            type: "global/getShareList",
            payload: {
                userId: this.props.userId?this.props.userId:"",
                shareType: 1,//分享给我
                text: "",
                startTime: "",
                endTime: "",
                pageSize: 10,
                pageNum: 1
            }
        }).then((res) => {
            console.log(res)
            if (res) {
                res.data.rows.forEach((item, index) => {
                    item.key = index
                })
                this.setState({ dataOfToMe: res.data.rows, total: res.data.total })
            }
        })
    }
    //分享给人
    getShareListFromMe = () => {
        const { dispatch, } = this.props
        dispatch({
            type: "global/getShareList",
            payload: {
                userId: this.props.userId?this.props.userId:"",
                shareType: 2,//我的分享
                text: "",
                startTime: "",
                endTime: "",
                pageSize: 10,
                pageNum: 1
            }
        }).then((res) => {
            // console.log(res)
            if (res) {
                res.data.rows.forEach((item, index) => {
                    item.key = index
                })
                this.setState({ dataOfFromMe: res.data.rows })
            }
        })
    }


    //点击Tab
    onTabClickEvent = (key, event) => {
        // const { dispatch, userInfo } = this.props
        // console.log(key, event)
        if (key === 1) {
            this.getShareListToMe()
        } else {
            this.getShareListFromMe()
        }
    }
    render() {
        let { dataOfFromMe, dataOfToMe, total } = this.state

        const routes = [
            {
                path: 'share',
                breadcrumbName: '内容分享',
            },
        ];
        return (
            <div>
                <div className={styles.pagetop}>
                    <span className={styles.pageheader}>
                        <Breadcrumb>
                            <Breadcrumb.Item><AppstoreTwoTone />资源分享</Breadcrumb.Item>
                            <Breadcrumb.Item>
                                <a href="/share">分享列表</a>
                            </Breadcrumb.Item>
                        </Breadcrumb>
                    </span>
                </div>
                <div style={{ background: "white" }}>
                    <Tabs defaultActiveKey="1" onTabClick={this.onTabClickEvent}>
                        <TabPane tab="分享给我" key="1">
                            <ReceiveForm getShareListToMe={this.getShareListToMe} dataOfToMe={dataOfToMe} total={total} />
                        </TabPane>
                        <TabPane tab="我的分享" key="2">
                            <TOShareForm getShareListFromMe={this.getShareListFromMe} dataOfFromMe={dataOfFromMe} />
                        </TabPane>
                    </Tabs>
                </div>
            </div>
        );
    }
}

export default ShareForm;